/**
 * html5调用摄像头扫码
 */
;(function (win, doc) {
    function QRScan(div_id) {
        this.div_id = div_id;
        this.div_can = null;
        this.videos = [];
        this.medioConfig = {};
        this.can_open = false;
        this.init();
    }

    QRScan.prototype = {
        init: function () {
            win.URL = (win.URL || win.webkitURL || win.mozURL || win.msURL);
            var promisifiedOldGUM = function (constraints) {
                var getUserMedia = (navigator.getUserMedia ||
                    navigator.webkitGetUserMedia || navigator.mozGetUserMedia);
                if (!getUserMedia) {
                    return Promise.reject(new Error('getUserMedia is not implemented in this browser'));
                }
                return new Promise(function (resolve, reject) {
                    getUserMedia.call(navigator, constraints, resolve, reject);
                });
            };
            if (navigator.mediaDevices === undefined) {
                navigator.mediaDevices = {};
            }
            if (navigator.mediaDevices.getUserMedia === undefined) {
                navigator.mediaDevices.getUserMedia = promisifiedOldGUM;
            }

            var self = this;
            self.div_can = doc.getElementById(self.div_id);
            navigator.mediaDevices.enumerateDevices().then(function (devices) {
                devices.forEach(function (dv) {
                    var kind = dv.kind;
                    if (kind.match(/^video.*/)) {
                        self.videos.push(dv.deviceId);
                        console.log(dv);
                    }
                });
                var len = self.videos.length;
                self.can_open = true;
                self.medioConfig = {
                    audio: false,
                    video: {deviceId: self.videos[len - 1]}
                }
            });
        },
        openScan: function () {
            var self = this;
            if (self.can_open) {
                var vd = doc.createElement('video');
                vd.setAttribute('id', 'video_id');
                navigator.mediaDevices.getUserMedia(self.medioConfig).then(function (stream) {
                    vd.srcObject = stream;
                    // vd.src = win.URL.createObjectURL(stream);
                    //  vd.srcObject = stream  在新的浏览器中需使用此代替createObjectURL
                    self.div_can.appendChild(vd);
                }).catch(function (err) {
                    var p = doc.createElement('p');
                    p.innerHTML = 'ERROR: ' + err +
                        '<br>该浏览器不支持调用摄像头，请使用百度浏览器';
                    self.div_can.appendChild(p);
                });
                vd.play();
            }
        },

        closeScan: function () {
            this.div_can.innerHTML = '';
        },
        // 截图上传
        getImgDecode: function (func, scanType) {
            var self = this;
            var video = doc.getElementById('video_id');
            var canvas = doc.createElement('canvas');
            canvas.width = 500;
            canvas.height = 500;
            var ctx = canvas.getContext('2d');
            ctx.drawImage(video, 0, 0, 500, 500);
            if (canvas.toBlob === undefined) {
                var base64 = canvas.toDataURL();
                var blob = self.Base64ToBlob(base64);
                self.sendBlob(blob, func, scanType);
            } else {
                canvas.toBlob(function (blob) {
                    self.sendBlob(blob, func, scanType);
                });
            }
        },

        sendBlob: function (blob, func, scanType) {
            var fd = new FormData();
            fd.append('scanType', scanType);
            fd.append('multiFile', blob);
            var xhr = new XMLHttpRequest();
            xhr.open('post', ctx + "barCodeAnalyzing/analyzing", true);
            xhr.onload = function () {
                if (xhr.status == 200) {
                    var data = xhr.responseText;   // 获取响应数据
                    var dataJson = JSON.parse(data);  // 使用JSON.parse()进行解析：使用JSON对象的静态方法parse()，将字符串解析为对象。
                    if (dataJson.type == "SUCCESS") {
                        var qrCodeMessage = dataJson.data; // 解析后的二维码
                        var jumpType = qrCodeMessage.substring(qrCodeMessage.lastIndexOf('|') + 1);
                        //如果是1就调取转序的跳转页面的接口
                        if (jumpType === '1') {
                            // var str = encodeURIComponent(qrCodeMessage.replace("/","◐").replace("/","◑"))+'';
                            // $('#code1').val(qrCodeMessage.replace("/","◐").replace("/","◑"));
                            location.href = ctx + 'result/' + scanType + '/' +encodeURIComponent(qrCodeMessage.replace("/","◐").replace("/","◑"));
                        }
                        //如果是2就调取装配的跳转页面的接口
                        else if (jumpType === '2') {
                            //location.href = ctx + 'assembly/' + scanType + '/' + encodeURIComponent(qrCodeMessage.replace("/","◐"));
                            location.href = ctx + 'assembly/' + scanType + '/' +encodeURIComponent(qrCodeMessage.replace("/","◐").replace("/","◑"));
                        }
                    }
                }
            };
            xhr.send(fd);
        },

        Base64ToBlob: function (base64) {
            var code = win.atob(base64.split(',')[1]);
            var len = code.length;
            var as = new Uint8Array(len);
            for (var i = 0; i < len; i++) {
                as[i] = code.charCodeAt(i);
            }
            return new Blob([as], {type: 'image/png'});
        }
    }

    win.QRScan = QRScan;
}(window, document));
